#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
# 
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998
# Netscape Communications Corporation. All Rights Reserved.
# 
# Contributor(s): TomWij <TomWij@live.com>

##############################################################################
#
# getStatistics.cgi
# -----------------
# Gets the statistics from the MySQL Table 'suggestions' in HTML table format.
#
##############################################################################

use DBI;
use DBD::mysql;

print "Content-type: text/html\n\n";

# Gets the statistics from the MySQL Table 'suggestions'.
do '..\..\..\..\localconfig';
$driver = DBI->install_driver("mysql"); 
$connect = $driver->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass, {}) or print "<b>MySQL: Connection failed.</b>";

$query = "SELECT bid, pid, cid, user, user_severity, algo_severity, choice_severity FROM suggestions ORDER BY bid DESC LIMIT 0, 100;";
$query_handle = $connect->prepare($query) or print "<b>MySQL: Query Preparation failed.</b>";
$query_handle->execute() or print "<b>MySQL: Query Execution failed.</b>";
$query_handle->bind_columns(undef, \$bid, \$pid, \$cid, \$user, \$user_severity, \$algo_severity, \$choice_severity);

# Prints the statistics in HTML table format.
print "<table style=\"border: solid 1px Black\" cellpadding=\"4\">";
print "<thead><tr><td>Bug</td><td>Product</td><td>Component</td><td>User</td><td>Severity by User</td><td>Severity by Algorithm</td><td>Chosen Severity</td></thead><tbody>";
while($query_handle->fetch()) {
  $bug_query = "SELECT short_desc FROM bugs WHERE bug_id = " . $bid;
  $bug_query_handle = $connect->prepare($bug_query) or print "<b>MySQL: Query Preparation failed.</b>";
  $bug_query_handle->execute() or print "<b>MySQL: Query Execution failed.</b>";
  $bug_query_handle->bind_columns(undef, \$description);
  while ($bug_query_handle->fetch()) {}
  
  $user_query = "SELECT profiles.realname, profiles.login_name, profiles.userid FROM logincookies JOIN profiles ON logincookies.userid = profiles.userid WHERE logincookies.cookie = '$user'";
  $user_query_handle = $connect->prepare($user_query) or print "<b>MySQL: Query Preparation failed.</b>";
  $user_query_handle->execute() or print "<b>MySQL: Query Execution failed.</b> $user_query";
  $user_query_handle->bind_columns(undef, \$fullname, \$loginname, \$uid);
  while ($user_query_handle->fetch()) {}

  print "<tr><td><a href=\"/show_bug.cgi?id=$bid\">$description</a></td><td><a href=\"http://localhost.com/editproducts.cgi?action=edit&product=$pid\">$pid</a></td><td><a href=\"http://localhost.com/editcomponents.cgi?action=edit&product=$pid&component=$cid\">$cid</a></td><td><a href=\"http://localhost.com/editusers.cgi?action=edit&userid=$uid\">$fullname ($loginname)</a></td><td>$user_severity</td><td>$algo_severity</td><td>$choice_severity</td></tr>";
}
print "</tbody></table>";